﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleTesrt.Algorithm
{
    class TouTou
    {
        static void Main6y6(string[] ae)
        {
            int[] arrint = new int[] { 1, 2, 7, 3, 4, 5, 0, 8, 6, 7, 9 };
            int sin = 7;
            arrint.Select((a, index) => new { a, index })
                                                        .Where(a => a.a == sin)
                                                        .ToList()
                                                        .ForEach((a) => { Console.WriteLine(a.index); });
            string[] source = new string[] { "12,bob", "3,sky", "6,cool", "1,good", "22,go" };
            for (int i = 0; i < source.Length; i++)
            {
                for (int j = i; j < source.Length; j++)
                {
                    if (Convert.ToInt32(Regex.Match(source[i], @"\d+").Value) > Convert.ToInt32(Regex.Match(source[j], @"\d+").Value))
                    {
                        string temp = source[i];
                        source[i] = source[j];
                        source[j] = temp;
                    }
                }
            }
            for (int i = 0; i < source.Length; i++)
            {
                Console.WriteLine(source[i]);
            }
            //先把字符串分解开，按照int类型排序，记录索引

            string[] strarrq = new string[] { "12,bob", "3,sky", "6,cool", "12,bob", "12,bob", "1,good", "aaa", "aaa", "22,go" };
            var sd = strarrq.Distinct().ToArray();
            for (int i = 0; i < sd.Length; i++)
            {
                Console.WriteLine(sd[i]);
            }
            methodByTOUTOU mt = new methodByTOUTOU();
            sin = mt.resu(arrint, r: sin);
            Console.WriteLine(sin);
            Console.WriteLine("-----------------------------------------------");
            Console.WriteLine(mt.jiecheng(4));
            Console.WriteLine(mt.Diguijiecheng(3, (a) => a));
            Console.WriteLine(mt.DiguiFBNQ(8,a=>a));
        }

    }
    public class methodByTOUTOU
    {
        public int resu(int[] arrint, int r)
        {
            int bbb = 0;
            for (int i = 0; i < arrint.Length; i++)
            {
                if (arrint[i] == r)
                    bbb = i;
                continue;

            }
            if (bbb == 0)
                return -1;
            else
                return bbb;
        }
        /// <summary>
        /// 普通的计算0-n的阶乘
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public int jiecheng(int n)
        {
            if (n == 0) return 1;
            return jiecheng(n - 1) * n;
        }
        /// <summary>
        /// 使用委托尾递归实现阶乘方法
        /// </summary>
        /// <param name="a"></param>
        /// <param name="abs"></param>
        /// <returns></returns>
        public int Diguijiecheng(int a, Func<int,int> abs)
        {
            if (a == 0) return abs(1);
            return Diguijiecheng(a - 1, q => abs(a * q));
        }
        //todo:实现了尾递归，其实是把原本应该分配在栈上的信息丢到了托管堆上。每个匿名方法其实都是托管堆上的对象，虽然说这种生存周期短的对象不会对内存资源方面造成多大问题，但是尽可能减少此类对象，对于性能肯定是有帮助的
        /// <summary>
        /// 使用尾递归计算斐波那契数列
        /// </summary>
        /// <param name="n"></param>
        /// <param name="fun"></param>
        /// <returns></returns>
        public int DiguiFBNQ(int n, Func<int, int> fun)
        {
            if (n < 2) return fun(n);
            return DiguiFBNQ(n - 1, a1 => DiguiFBNQ(n - 2, a2 => fun(a1 + a2)));
        }
        static int chuanfun(int a)
        {
            if (a < 2) return a;//1/2直接return
            return chuanfun(a - 1) + chuanfun(a - 2);
        }
        static int funzhao(int a, int a1, int a2)
        {
            if (a == 0) return a1;
            return funzhao(a - 1, a2, a1 + a2);
        }
    }
}
